<?php

class Model_Person extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
        $this->Member_like = getModel('Model_MemberLike');
        $this->Product = getModel('Model_Product');
        $this->ProductReports = getModel('Model_ProductReports');
        $this->Member = getModel('Model_Member');
    }

    function get_user($mid, $uid, $username) {
        if (!empty($uid)) {
            $user = $this->Member->getMember($uid);
        } else {
            $user = $this->Member->getMember(0, '', $username);
            $uid = $user['result'][0]['id'];
        }
        if(empty ($user['result'])) showMsgCode("用戶不存在！", "/");
        $_mem = $this->Member->getMemberCondition($uid);

        $_body_skin = empty($_mem['result'][0]['body_skin']) ? $this->Lookup->getLookup('face_skin', 1) : $this->Lookup->getLookup('face_skin', $_mem['result'][0]['face_skin']);
        $user['result'][0]['body_skin'] = $_body_skin['result'][0]['cname'];
        $_likes = $this->Member_like->getAllLikeMember(0, $uid, 1, 200);
        $user['result'][0]['likes'] = count($_likes);
        if (!empty($mid)) {
            $_islike = $this->Member_like->getMemberLike($mid, 'member', $uid);
        } else {
            $_islike = 0;
        }
        $user['result'][0]['islike'] = !empty($_islike) ? 1 : 0;
        $user['result'][0]['photo_l'] = imore_avatar($user['result'][0]['photo']);
        return $user;
    }

    function get_user_report($user_reports_res,$username = '') {
        $user_reports = array();
        if ($user_reports_res['result']) {
            foreach ($user_reports_res['result'] as $k => $v) {
                if (intval($v['is_blog']) == 0) {
                    $product_info = $this->Product->getProductById($v['oid']);
                    if (!empty($product_info)) {
                        $user_reports[$k]['product']['id'] = $v['oid'];
                        $user_reports[$k]['product']['name'] = $product_info['name'];
                        $user_reports[$k]['product']['cname'] = $product_info['cname'];
                        $user_reports[$k]['product']['type_name'] = $product_info['type_name'];
                        $user_reports[$k]['product']['photo'] = PhotoResize($product_info['cover'], 116, 116);
                        $user_reports[$k]['product']['member_type'] = $product_info['member_type'];
                        $user_reports[$k]['product']['good'] = $product_info['product_reports_stat']['good'];
                        $user_reports[$k]['product']['common'] = $product_info['product_reports_stat']['common'];
                        $user_reports[$k]['product']['bad'] = $product_info['product_reports_stat']['bad'];

                        if (!empty($product_info['vote_option_data']['result'])) {
                            foreach ($product_info['vote_option_data']['result'] as $k1 => $v1) {
                                $user_reports[$k]['report']['category'][$k1]['k'] = $k1;
                                $user_reports[$k]['report']['category'][$k1]['name'] = $v1['cname'];
                                $user_reports[$k]['report']['category'][$k1]['value'] = @$v['category_score' . ($k1 + 1)];
                            }
                        }
                    }
                    $user_reports[$k]['report']['share_url'] = optimize_url('product', 'reportDetail', 'mtype=' . $product_info['member_type'] . '&pid=' . $v['oid'] . '&rid=' . $v['id']);
                    $user_reports[$k]['report']['content'] = json_decode($v['content_json'], true);
                    $user_reports[$k]['report']['body'] = $v['body'];
                } else {
                    $vote_option_data = array('result' => array('滿意度', '抵買度', '舒適度', '清爽度', '持久度', '外型')); //好評  美白度
                    foreach ($vote_option_data['result'] as $k1 => $v1) {
                        if(!empty ($v['category_score' . ($k1 + 1)])){
                        $user_reports[$k]['report']['category'][$k1]['k'] = $k1;
                        $user_reports[$k]['report']['category'][$k1]['name'] = $v1;
                        $user_reports[$k]['report']['category'][$k1]['value'] = @$v['category_score' . ($k1 + 1)];
                        }
                    }
                    $user_reports[$k]['report']['body'] = htmlspecialchars_decode($v['body']);
                    $user_reports[$k]['report']['share_url'] = optimize_url('person', 'blog', 'username=' . $username . '&bid=' . $v['id']);
                    $user_reports[$k]['report']['comments'] = db_query_count('product_reports_comment', "oid=" . mysql_escape_string($v['id']) . " and status=1");
                }
                $user_reports[$k]['report']['id'] = $v['id'];
                $user_reports[$k]['report']['title'] = $v['title'];
                $user_reports[$k]['report']['likes'] = $v['likes'];
                $user_reports[$k]['report']['well'] = $v['well'];
                $user_reports[$k]['report']['is_blog'] = $v['is_blog'];
                //$user_reports[$k]['report']['time'] = ($v['lastmodify'] && strtotime($v['lastmodify']) >0)?$v['lastmodify']:$v['addtime'];
                $user_reports[$k]['report']['time'] = $v['addtime'];
                //echo strtotime($v['lastmodify']);
            }
        }
        return $user_reports;
    }

    function get_userlike($uid, $page, $ps) {
        $data = $this->Member_like->getAllLikeMember($uid, 0, $page, $ps);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $data[$k]['reports_count'] = db_query_count('product_reports', 'mid=' . mysql_escape_string($v['sid']) . ' and status=1');
                $data[$k]['recommen_count'] = db_query_count('member_like', "section='report' and mid=" . mysql_escape_string($v['sid']));
                $data[$k]['photo_l'] = imore_avatar($v['photo']);
            }
        }
        return $data;
    }

    function get_likeuser($uid, $page, $ps) {
        $data = $this->Member_like->getAllLikeMember(0, $uid, $page, $ps);
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $_mem1 = $this->Member->getMember($v['mid']);

                $data[$k]['_name'] = $_mem1['result'][0]['username'];
                $data[$k]['reports_count'] = db_query_count('product_reports', 'mid=' . mysql_escape_string($v['mid']) . ' and status=1');
                $data[$k]['recommen_count'] = db_query_count('member_like', "section='report' and mid=" . mysql_escape_string($v['mid']));
                $data[$k]['photo_l'] = imore_avatar($_mem1['result'][0]['photo']);
                $data[$k]['member_type'] = $_mem1['result'][0]['member_type'];
            }
        }
        return $data;
    }

    function get_user_like_report($uid, $page, $pagesize) {
        $user_report_res = $this->Member_like->getMemberLike($uid, 'report', 0, $page, $pagesize);
        $user_reports = $_temp = array();
        if ($user_report_res) {
            foreach ($user_report_res as $k => $v) {
                $_reports = $this->ProductReports->getReportsList(0, 0, $v['sid']);
                if (!empty($_reports['result'][0])) {
                    $_product_info = $this->Product->getProductById($_reports['result'][0]['oid']);
                    if (!empty($_product_info)) {
                        $user_reports[$k]['product']['id'] = $_reports['result'][0]['oid'];
                        $user_reports[$k]['product']['name'] = $_product_info['name'];
                        $user_reports[$k]['product']['cname'] = $_product_info['cname'];
                        $user_reports[$k]['product']['type_name'] = $_product_info['type_name'];
                        $user_reports[$k]['product']['photo'] = PhotoResize($_product_info['cover'], 116, 116);
                        $user_reports[$k]['product']['good'] = $_product_info['product_reports_stat']['good'];
                        $user_reports[$k]['product']['common'] = $_product_info['product_reports_stat']['common'];
                        $user_reports[$k]['product']['bad'] = $_product_info['product_reports_stat']['bad'];
                        $user_reports[$k]['product']['member_type'] = $_product_info['member_type'];

                        $user_reports[$k]['report']['id'] = $_reports['result'][0]['id'];
                        $user_reports[$k]['report']['title'] = $_reports['result'][0]['title'];
                        $user_reports[$k]['report']['body'] = wordscut($_reports['result'][0]['body'], 100);
                        $user_reports[$k]['report']['likes'] = $_reports['result'][0]['likes'];
                        $user_reports[$k]['report']['well'] = $_reports['result'][0]['well'];
                        $user_reports[$k]['report']['share_url'] = optimize_url('product', 'reportDetail', 'mtype=' . $_product_info['member_type'] . '&pid=' . $_reports['result'][0]['oid'] . '&rid=' . $_reports['result'][0]['id']);

                        if (!empty($_product_info['vote_option_data']['result'])) {
                            foreach ($_product_info['vote_option_data']['result'] as $k1 => $v1) {
                                $user_reports[$k]['report']['category'][$k1]['k'] = $k1;
                                $user_reports[$k]['report']['category'][$k1]['name'] = $v1['cname'];
                                $user_reports[$k]['report']['category'][$k1]['value'] = $_reports['result'][0]['category_score' . ($k1 + 1)];
                            }
                        }

                        $_temp = json_decode($_reports['result'][0]['content_json'], true);
                        $ii = 1;
                        if (!empty($_temp)) {
                            foreach ($_temp as $k2 => $v2) {
                                $user_reports[$k]['report']['content'][$ii]['p'] = PhotoResize($v2['p'], 163, 102);
                                $user_reports[$k]['report']['content'][$ii]['t'] = $v2['t'];
                                $ii++;
                            }
                        }
                    }
                    //$user_reports[$k]['report']['time'] = ($_reports['result'][0]['lastmodify'] && strtotime($_reports['result'][0]['lastmodify']) >0)?$_reports['result'][0]['lastmodify']:$_reports['result'][0]['addtime'];
                    $user_reports[$k]['report']['time'] = $_reports['result'][0]['addtime'];
                }
            }
        }
        return $user_reports;
    }

}

?>